package com.example.cd5160866.urlapp.utils;

import android.graphics.Bitmap;

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

public class LruDiskCache {

    public static final int DEFAULT_BUFFER_SIZE = 32 * 1024;// 32kb

    public static final Bitmap.CompressFormat DEFAULT_COMPRESS_FORMAT = Bitmap.CompressFormat.PNG;

    public static final int DEFAULT_COMPRESS_QUALITY = 100;

    protected DiskLruCache cache;

    private static final String ERROR_ARG_NULL = " argument must be not null";
    private static final String ERROR_ARG_NEGATIVE = " argument must be positive number";

    /**
     *
     * @param cacheDir
     *            缓存路径
     * @param cacheMaxSize
     *            缓存文件大小
     * @param cacheMaxFileCount
     *            缓存文件数目
     * @throws IOException
     */

    public LruDiskCache(File cacheDir, long cacheMaxSize, int cacheMaxFileCount)
            throws IOException {
        if (cacheDir == null) {
            throw new IllegalArgumentException("cacheDir" + ERROR_ARG_NULL);
        }
        if (cacheMaxSize < 0) {
            throw new IllegalArgumentException("cacheMaxSize"
                    + ERROR_ARG_NEGATIVE);
        }
        if (cacheMaxFileCount < 0) {
            throw new IllegalArgumentException("cacheMaxFileCount"
                    + ERROR_ARG_NEGATIVE);
        }
        if (cacheMaxSize == 0) {
            cacheMaxSize = Long.MAX_VALUE;
        }
        if (cacheMaxFileCount == 0) {
            cacheMaxFileCount = Integer.MAX_VALUE;
        }
        initCache(cacheDir, cacheMaxSize, cacheMaxFileCount);
    }

    /**
     *
     * @param cacheDir
     *            缓存路径
     * @param cacheMaxSize
     *            缓存文件大小
     * @param cacheMaxFileCount
     *            缓存文件数量
     * @throws IOException
     */
    private void initCache(File cacheDir, long cacheMaxSize,
                           int cacheMaxFileCount) throws IOException {
        try {
            // 获取DiskLruCache
            cache = DiskLruCache.open(cacheDir, 1, 1, cacheMaxSize,
                    cacheMaxFileCount);
        } catch (IOException e) {
            e.printStackTrace();
            if (cache == null) {
                throw e; // new
                // RuntimeException("Can't initialize disk cache",e);
            }
        }
    }

    /**
     *
     * @return 缓存路径
     */
    public File getDirectory() {
        return cache.getDirectory();
    }

    /**
     * 根据key去获取文件存储目录
     *
     * @param key
     * @return
     */
    public File get(String key) {
        DiskLruCache.Snapshot snapshot = null;

        try {
            snapshot = cache.get(key);
            return snapshot == null ? null : snapshot.getFile(0);
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        }
    }

    /**
     * 根据key去删除文件
     * @param key
     * @return
     */
    public boolean remove(String key) {
        try {
            return cache.remove(key);
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        }
    }

    /**
     * 保存文件到硬盘
     *
     * @param key
     * @param imageStream
     * @return
     * @throws IOException
     */
    public boolean save(String key, InputStream imageStream) throws IOException {
        DiskLruCache.Editor editor = cache.edit(key);
        if (editor == null) {
            return false;
        }
        OutputStream os = new BufferedOutputStream(editor.newOutputStream(0),
                DEFAULT_BUFFER_SIZE);
        int count;
        byte[] bs = new byte[DEFAULT_BUFFER_SIZE];
        while ((count = imageStream.read(bs, 0, DEFAULT_BUFFER_SIZE)) != -1) {
            os.write(bs, 0, count);
        }
        os.flush();
        editor.commit();
        return true;
    }
}
